<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class jQuickForm_Element_Slider_Range extends HTML_QuickForm2_Container_Group {
    public $range_min=0;
    public $range_max=100;
    public $range_step=1;

    public function setValue($value){
        jQuickForm::js()->addJsOnload('
        jQuery("#'.$this->getId().'_jqfslider").slider( "values" , '.json_encode(array_values($value)).' );
        ');
        return parent::setValue($value);
    }

    public function render(HTML_QuickForm2_Renderer $renderer){
        $name = $this->getName();
        $id   = $this->getId();
//        $renderer = new HTML_QuickForm2_Renderer_Default();
        $renderer->setElementTemplateForGroupClass(strtolower(get_class($this)),'html_quickform2_element','<td>{element}</td>');
        $renderer->setTemplateForClass(strtolower(get_class($this)),'
                <div class="jqf_row">
        <label for="{id}" class="jqf_element"><qf:required><span class="required">* </span></qf:required>{label}</label>
        <div id="{id}_jqfslider" class="jqfslider"></div>
        <div class="jqf_slider_header">
        с {comment}{content}<div class="clear"></div></div>
        </div>');


        $value = $this->getValue();
        jQuickForm::js()->needJquery();
        jQuickForm::js()->needJqueryUi();
        jQuickForm::js()->addJsOnload('
             /*-------------- jQuickForm_SliderRange ---------------*/
            jQuery("#'.$name.'_min").change(function(){
                jQuery("#'.$this->getId().'_jqfslider").slider( "value" , jQuery(this).val() );
            });
            jQuery("#'.$id.'_jqfslider").slider({
                range: true,
                values: '.json_encode(array_values($value)).',
                min: '.$this->range_min.',
                max: '.$this->range_max.',
                step: '.$this->range_step.',
                slide: function(event, ui) {
                    jQuery("#'.$name.'_min").val(ui.values[0]);
                    jQuery("#'.$name.'_max").val(ui.values[1]);
            	},
                change: function(event, ui) {
                    jQuery("#'.$name.'_min").val(ui.values[0]);
                    jQuery("#'.$name.'_max").val(ui.values[1]);
                }
            });
             /*-------------- jQuickForm_SliderRange ---------------*/
        ');
        return parent::render($renderer);
    }
}
?>